package com.iwop.moto.service.impl;

import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.AuditHistoryService;
import com.iwop.moto.service.BaseService;
import com.iwop.moto.utils.stringUtil.DateFormatter;
import com.iwop.moto.utils.stringUtil.ObjectId;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @DB_table F_AUDIT_HISTORY 审核历史记录表
 * @author:  liuwb by 2016-09-03 on create
 * @explain: 用户审核 or 商品审核历史表 audit_type 审核类型:[User 用户] [Goods 商品]
 */
@Service
public class AuditHistoryImpl extends BaseService implements AuditHistoryService {

    /**
     * @author: liuwb by 2016-09-03 on create
     * @explain: 新增 审核记录
     * @param SRModel 数据集
     */
    public void insert(SuperRecordModel SRModel){
        getSqlSession().insert("FAuditHistoryMapper.insert",SRModel);
    }

    /**
     * @author: liuwb by 2016-09-22 create
     * @explain: 查询用户审核历史记录
     * @param SRModel userId 用户id
     */
    public List<SuperRecordModel> findUserAuditHistory(SuperRecordModel SRModel){
        return getSqlSession().selectList("FAuditHistoryMapper.findUserAuditHistory",SRModel);
    }

    /**
     * @author: liuwb by 2016-09-28 create
     * @explain: 查询出最近一条，根据审核人外键和类型
     * @param SRModel audit_fkey and audit_type
     */
    public SuperRecordModel findHistoryByFkeyType (SuperRecordModel SRModel){
        return getSqlSession().selectOne("FAuditHistoryMapper.findHistoryByFkeyType",SRModel);
    }

    /**
     * @author liuwb by 2016-09-27 create
     * @explain 维修店发起 插入审核记录
     * @param SRModel
     */
    public void inserStoreAuditHistory(SuperRecordModel SRModel){
        SuperRecordModel userCreate = new SuperRecordModel();
        userCreate.put("sid", ObjectId.UUID());
        userCreate.put("audit_type", "User");
        userCreate.put("audit_fkey", SRModel.getString("sid"));
        userCreate.put("audit_time", DateFormatter.getDateAddSecond(1));
        if((boolean)SRModel.get("isCreate")==false){
            userCreate.put("audit_describe", "重新申请");
        } else {
            userCreate.put("audit_describe", "发起申请");
        }
        insert(userCreate);
        /**
         *  查询维修店上级服务商列表
         *  将维修店按比例分配给服务商
         **/
        List<SuperRecordModel> sers = getSqlSession().selectList("FAuditHistoryMapper.findStoreUpCityService",SRModel);
        /** TeamMember 团队成员分配，需与M_USER 的审核状态做逻辑关联 当审核全部通过(状态为2)的时候出现在团队成员 **/
        SuperRecordModel TeamMember = new SuperRecordModel();
        TeamMember.put("sid",ObjectId.UUID());
        TeamMember.put("user_fkey",SRModel.getString("sid"));
        TeamMember.put("status_time",new Date());
        if(sers != null && sers.size() > 0){
            TeamMember.put("status_flag","1");//有服务商，标识为正常状态
            SuperRecordModel serviceCreate = new SuperRecordModel();
            serviceCreate.put("sid",ObjectId.UUID());
            serviceCreate.put("audit_type", "Service");
            serviceCreate.put("audit_fkey", SRModel.getString("sid"));
            serviceCreate.put("audit_status","0");
            serviceCreate.put("audit_time", DateFormatter.getDateAddSecond(2));
            if(sers.size()==1){//如果只有一个服务商默认分配给该服务商
                serviceCreate.put("audit_user_id", sers.get(0).getString("user_id"));
                serviceCreate.put("audit_user_name",sers.get(0).getString("nickname"));
                TeamMember.put("user_pkey",sers.get(0).getString("user_id"));
            } else {//如果有多个服务商则分配给成员最少
                List<String> paramIds = new ArrayList<>();
                for(SuperRecordModel srm : sers){
                    paramIds.add(srm.getString("user_id"));
                }
                SRModel.put("paramIds",paramIds);
                // 查询出 user_pkey 参数 paramIds sid 列表
                SuperRecordModel minTeam = getSqlSession().selectOne("FTeamMemberMapper.findTeamMemberCount",SRModel);
                serviceCreate.put("audit_user_id",minTeam.getString("sid"));
                serviceCreate.put("audit_user_name",minTeam.getString("nickname"));
                TeamMember.put("user_pkey",serviceCreate.getString("audit_user_id"));
            }
            insert(serviceCreate);
        } else {
            TeamMember.put("status_flag","2");//没有服务商，标识为2，等待平台分配
        }
        getSqlSession().insert("FTeamMemberMapper.insert",TeamMember);//维修店所属服务商
        SuperRecordModel moto = new SuperRecordModel();
        moto.put("sid",ObjectId.UUID());
        moto.put("audit_type", "Moto");
        moto.put("audit_fkey", SRModel.getString("sid"));
        moto.put("audit_status","0");
        moto.put("audit_time", DateFormatter.getDateAddSecond(4));
        insert(moto);
    }

    /**
     * @author liuwb by 2016-09-28 create
     * @explain 服务商发起 插入审核记录
     * @param SRModel
     */
    public void inserServiceAuditHistory(SuperRecordModel SRModel){
        SuperRecordModel userCreate = new SuperRecordModel();
        userCreate.put("sid", ObjectId.UUID());
        userCreate.put("audit_type", "User");
        userCreate.put("audit_fkey", SRModel.getString("sid"));
        userCreate.put("audit_time", DateFormatter.getDateAddSecond(1));
        if((boolean)SRModel.get("isCreate")==false){
            userCreate.put("audit_describe", "重新申请");
        } else {
            userCreate.put("audit_describe", "发起申请");
        }
        insert(userCreate);
        SuperRecordModel moto = new SuperRecordModel();
        moto.put("sid",ObjectId.UUID());
        moto.put("audit_type", "Moto");
        moto.put("audit_fkey", SRModel.getString("sid"));
        moto.put("audit_status","0");
        moto.put("audit_time", DateFormatter.getDateAddSecond(2));
        insert(moto);
    }

    /**
     * @author: liuwb by 2016-09-28 create
     * @explain:  更新审核状态和审核记录
     * @param SRModel
     */
    public void updateAuditStatusByid(SuperRecordModel SRModel){
        getSqlSession().update("FAuditHistoryMapper.updateAuditStatusByid",SRModel);
    }

    /**
     * @author: liuwb by 2016-09-28 create
     * @explain: 删除
     * @param SRModel
     */
    public void deleteAuditByParam (SuperRecordModel SRModel){
        getSqlSession().delete("FAuditHistoryMapper.deleteAuditByParam",SRModel);
    }


}
